package com.chow.test;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.Calendar;

import org.htmlparser.Parser;
import org.htmlparser.Tag;
import org.htmlparser.visitors.NodeVisitor;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class LoginRR {

	static KVPair[] h = new KVPair[] {
			new KVPair("Host", "www.renren.com"),
			new KVPair(
					"User-Agent",
					"Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.15) Gecko/20110303 Firefox/3.6.15"),
			new KVPair("Accept",
					"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"),
			new KVPair("Accept-Language", "zh-cn,zh;q=0.5"),
			new KVPair("Accept-Encoding", "deflate"),
			new KVPair("Accept-Charset",
					"ISO-8859-1,GB2312,utf-8;q=0.7,*;q=0.7"),
			new KVPair("Keep-Alive", "115"),
			new KVPair("Connection", "keep-alive"),
			new KVPair("Referer", "http://renren.com/SysHome.do") };

	HttpEngine eng = new HttpEngine();

	public static void redirect(HttpEngine eng, String location)
			throws Exception {
		System.out.println("redirect to: " + location);
		// HttpURLConnection.setFollowRedirects(false);
		eng.get(location, h, null);
		int code = eng.getResponseCode();

		if (code == 302) {
			String loc = eng.getHeaderField("location");
			if (!loc.startsWith("http")) {
				URL url = new URL(location);
				loc = url.getProtocol() + "://" + url.getHost() + loc;
			}
			redirect(eng, loc);
		}

		if (code == 200) {
			System.out.println(eng.getResponseHtml());
		}
	}

	public static void main(String[] args) throws Exception {
		// String sUrl = "http://www.renren.com/PLogin.do";
		//
		// KVPair[] pairs = new KVPair[] {
		// new KVPair("email", "hchwang001@163.com"),
		// new KVPair("password", "Aj@x12345"),
		// new KVPair("origURL", "http://www.renren.com/SysHome.do"),
		// new KVPair("domain", "renren.com") };
		//
		// HttpEngine eng = new HttpEngine();
		// HttpURLConnection.setFollowRedirects(false);
		// eng.post(sUrl, h, pairs);
		// int code = eng.getResponseCode();
		// if (code == 302) {
		// String loc = eng.getHeaderField("location");
		// if (!loc.startsWith("http")) {
		// URL url = new URL(loc);
		// loc = url.getProtocol() + "://" + url.getHost() + loc;
		// }
		// redirect(eng, loc);
		// }

		// HttpURLConnection.setFollowRedirects(false);
		//
		LoginRR rr = new LoginRR();
		rr.eng.get("http://www.renren.com", null, null);
		System.out.println(rr.eng.getResponseHtml());
		// rr._login();
	}

	private void _login() throws Exception {
		KVPair[] ps = new KVPair[] {
				new KVPair("email", "accountbase@163.com"),
				new KVPair("password", "Aj@x12345"),
				new KVPair("origURL", "http://www.renren.com/home"),
				new KVPair("domain", "renren.com") };

		String sUrl = "http://www.renren.com/PLogin.do";
		this.eng.post(sUrl, h, ps);
		int code = this.eng.getResponseCode();

		if (code == 302) {
			String loc = this.eng.getHeaderField("location");
			redirect(loc);
		}
	}

	private void redirect(String location) throws Exception {
		System.out.println("redirect to: " + location);

		this.eng.get(location, h, null);
		int code = this.eng.getResponseCode();

		if (code == 302) {
			String loc = this.eng.getHeaderField("location");
			if (!loc.startsWith("http")) {
				URL url = new URL(location);
				loc = url.getProtocol() + "://" + url.getHost() + loc;
			}
			redirect(loc);
		}
	}

}
